/*
 * @Author: Wang Shuo wangshuo@sgsimulation.com
 * @Date: 2024-07-19 16:44:54
 * @LastEditors: Du Weixing duweixing@sgsimulation.com
 * @LastEditTime: 2024-12-06 22:15:48
 * @Description:
 *
 * Copyright (c) 2024 by 神工仿真, All Rights Reserved.
 */
#include "LSectionNumericalCalculator.h"

#include "DataStructure/Common/Real.h"

#include "SectionCalculator/StandardSectionTool.h"

using SG::Algebra::Point;
using SG::DataStructure::Common::Real;
using SG::DataStructure::FEM::PBARLData;
using SG::FEMSolver::Element::LSectionNumericalCalculator;

void LSectionNumericalCalculator::Mesh (const PBARLData& section, _OUT Meshes& meshes, int refineLevel) const
{
    Real w1{ section.m_dimension[0] };
    Real w2{ section.m_dimension[1] };
    Real t1{ section.m_dimension[2] };
    Real t2{ section.m_dimension[3] };
    Real wf{ w1 - t2 };
    Real hw{ w2 - t1 };

    int n{ refineLevel + 1 };
    int nwf{ 2 * n };
    int nhw{ 2 * n };
    int nt1{ n };
    int nt2{ n };

    StandardSectionTool secMesh;
    Point               point;
    int                 nodeId{ 0 };
    int                 eleId{ 0 };
    Meshes              newMeshes;
    NodeSet             newEdgeNodes;
    int                 numRepeatNodes{ 0 };
    NodeSet             edgeNodes;

    // 1st block
    point.m_y = w1;
    point.m_z = 0.0;
    secMesh.MeshRect (point, -wf, t1, nwf, nt1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 2nd block
    point.m_y = 0.0;
    secMesh.MeshRect (point, t2, t1, nt2, nt1, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt1 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);

    // 3rd block
    point.m_z = w2;
    secMesh.MeshRect2 (point, t2, -hw, nt2, nhw, nodeId, eleId, _OUT newMeshes, _OUT newEdgeNodes);
    numRepeatNodes = 2 * nt2 + 1;
    secMesh.Merge (edgeNodes, numRepeatNodes, newMeshes, meshes, nodeId, eleId);
    secMesh.UpdateEdgeNodes (newEdgeNodes, numRepeatNodes, edgeNodes);
}